package com.wise.service.cms.content.successarticle;

import com.wise.entities.content.SuccessArticle;
import com.wise.entities.content.SuccessArticleColumn;
import com.wise.framework.dao.SqlSelect;
import com.wise.framework.daomodel.DaoOne;
import com.wise.framework.daomodel.DaoPageList;
import com.wise.framework.restful.PageResultList;
import com.wise.summer.v1.cms.content.successarticles.ListSuccessArticles;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.summerframework.model.SummerService;
import org.summerframework.model.SummerServiceBean;

import static com.wise.model.content.ContentErrMsgs.Err_001600250014;

@Service
@SummerService
public class ListSuccessArticlesService implements SummerServiceBean<ListSuccessArticles> {
    @Override
    public void sum(ListSuccessArticles model) {
        SqlSelect sql = SqlSelect.selec("*")
                .fromClassName(SuccessArticle.class)
                .like(!StringUtils.isEmpty(model.getTitle()), SuccessArticle._title, "%" + model.getTitle() + "%")
                .eq(!StringUtils.isEmpty(model.getColumnId()), SuccessArticle._columnId, model.getColumnId())
                .eq(model.getState() != null, SuccessArticle._state, model.getState())
                .eqLang(SuccessArticle._lang)
                .order_by("id desc")
                .order_by(SuccessArticle._sort);

        PageResultList<SuccessArticle> pageResultList = DaoPageList.s(SuccessArticle.class, sql, model.getStart(), model.getLimit());

        pageResultList.getList().forEach(article -> {
            String columnName = DaoOne.s(SuccessArticleColumn.class, article.getColumnId(), Err_001600250014).getColumnName();
            article.setColumnName(columnName);
        });

        model.setSummerResult(pageResultList);
    }

    /**
     * 查询栏目数据
     */
//    private List<ArticleColumn> findColumn(List list){
//        SqlSelect sqlSelect = SqlSelect.selec("*");
//        sqlSelect.fromClassName(ArticleColumn.class);
//        sqlSelect.in("id",list);
//        List<ArticleColumn> articleColumns = DaoList.s(ArticleColumn.class,sqlSelect);
//        return articleColumns;
//    }
}
